﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.WebFactory
{
    public class CategoryPage : PublicPage<Category>
    {
        
        /// <summary>
        /// 页码
        /// </summary>
        protected int PageNo
        {
            get
            {
                if (Request.QueryString["PageNo"] != null)
                    return  int.Parse(Request.QueryString["PageNo"]);
                else if (Page.RouteData.Values.ContainsKey("PageNo"))
                {
                    return Convert.ToInt32(Page.RouteData.Values["PageNo"]);
                }
                else
                {
                    return 1;
                }
            }
        }

        #region 重写事件
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            //this.DataBind();
            if (this.Model == null)
                Response.Close();
        }

        #region 重写Render
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder winner = new StringBuilder();
            using (TextWriter tempWriter = new StringWriter())
            {
                base.Render(new HtmlTextWriter(tempWriter));
                winner.Append(tempWriter.ToString());
            }
            winner.Replace("[CategoryId]", CategoryId.ToString());
            winner.Replace("[ModelId]", this.Model.ModelId.ToString());


            IList<string> matchresult;

            #region 通用模型标签
            matchresult = TagEngine.TagCollection(winner.ToString(), "Slide");
            foreach (string nextmatch in matchresult)
            {
                winner.Replace(nextmatch, PageCommon.Slide(nextmatch));
            }
          
     
            matchresult = TagEngine.TagCollection(winner.ToString(), "PageList");
            foreach (string nextmatch in matchresult)
            {
                winner.Replace(nextmatch, PageCommon.CategoryPageList(winner, this.Model, this.PageNo));
            }
            #endregion
  
            CategoryCommon.Path(winner, CategoryId);


            PageCommon.RepalceFiled(winner, this.Model);

            if (Request["Method"] != null)
            {

                string path = StaticFilePath.CategoryUrl(this.Model, this.PageNo);
                new FileIO().WriterFile(HttpServer.MapPath(path), winner.ToString());
            }
            else
            {
                writer.Write(winner.ToString());
            }
        }
        #endregion

    }
}